package cn.dhn_app.horseman_app.service.impl;

import cn.dhn_app.horseman_app.service.GetPhoneCodeService;
import cn.itcast.util.ResponseEntity;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 12:57 2020/8/25
 * @Modified By:
 */
@Service
@Slf4j
public class GetPhoneCodeServiceimpl implements GetPhoneCodeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseEntity getCode(String phone) {
        if(phone.equals("")){
            return new ResponseEntity(300,"手机号码不能为空!");
        }
        Boolean ifAbsent = stringRedisTemplate.opsForValue().setIfAbsent(phone + "code", "6666");
        stringRedisTemplate.expire(phone + "code",60, TimeUnit.SECONDS);
        if (!ifAbsent){
            return new ResponseEntity(302,"已发送验证码,如未收到,请一分钟后重试!");
        }
        int num=(int)(Math.random()*8999)+1000;
        int i = alibabaPhoneCodePackaging(phone, num);
        if (i==200){
            return new ResponseEntity(200,"短信发送成功",num);
        }else if (i==400){
            return new ResponseEntity(400,"短信发送失败");
        }else {
            return new ResponseEntity(400,"短信发送失败");
        }

    }


    /**
     *
     * @Description: 阿里巴巴短信功能
     *
     * @auther: 张松
     * @date: 15:37 2020/8/25
     * @param: [phone, num]
     * @return: int
     *
     */
    private int alibabaPhoneCodePackaging(String phone,int num){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Ff8L6v5dqjTn8ymKLYs", "2JKuV67WyVhVbCRSmWpJTgbfO9aEkS");//广东速天
//        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIslHoiezqoXC6", "0QsOie6oOKdh207FG15734bQ2emPrL"); 杭州思软
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "大黄牛智能回收");
        request.putQueryParameter("TemplateCode", "SMS_177480265");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+num+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.debug(response.getData());
            JSONObject jsonObject = JSONObject.parseObject(response.getData());
            if(jsonObject.get("Code").equals("OK")){
                stringRedisTemplate.opsForValue().set(phone+"code",String.valueOf(num),60,TimeUnit.SECONDS);
                return 200;
            }else{
                return 400;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return 400;

    }
}
